//
//  GADropViewController.m
//  Gunterapple
//
//  Created by 热带雨林 on 2021/1/22.
//  Copyright © 2021 热带雨林. All rights reserved.
//

#import "GADropViewController.h"
#import "GDCollectionWaterView.h"
#import "GDWaterFallBuilder.h"
#import "GDCollectionViewWaterFlowLayout.h"
#import "armImageModel.h"
#import "GDWaterDetailViewController.h"

#define LOAD_COUNT 20

@interface GADropViewController ()<GDCollectionWaterViewDelegate>

@property (nonatomic, strong) GDCollectionWaterView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation GADropViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"Drop";
    [self setUpUI];
}

- (void)setUpUI{
    UIView *thisView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, GDScreenW, 0)];
    [self.view addSubview:thisView];
    NSArray *imageArray = [NSArray gg_arrayModelWithPlistName:@"ArmSizeImage.plist" withClassName:@"armImageModel"];
    [self.dataArray addObjectsFromArray:imageArray];
//    float array[LOAD_COUNT];
    NSMutableArray *testImagesScale = @[].mutableCopy;
//    for (int i=0; i<LOAD_COUNT; i++) {
//        CGFloat imageWidth = (50. + arc4random()%150);
////        CGFloat imageHeight = 100.;
//        CGFloat imageHeight = (100. + arc4random()%300);
//        array[i] = imageWidth/imageHeight;
//        [testImagesScale addObject:@(array[i])];
//    }
    for (int i = 0 ; i < self.dataArray.count; i ++) {
        armImageModel *model = self.dataArray[i];
        CGFloat imageWidth = model.width.floatValue;
        CGFloat imageHeight = model.height.floatValue;
        [testImagesScale addObject:@(imageWidth / imageHeight)];
    }

    GDCollectionViewWaterFlowLayout *layout = [[GDCollectionViewWaterFlowLayout alloc]init];
    self.collectionView = [[GDCollectionWaterView alloc]initWithFrame:GDScreenBounds collectionViewLayout:layout];
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.GDdelegate = self;
    [self.view addSubview:self.collectionView];
    
    NSArray *sizes = [GDWaterFallBuilder generateSizesWithScales:testImagesScale];
    [self.collectionView.cacheSizes addObjectsFromArray:sizes];
//    for (int i=0; i<LOAD_COUNT; i++) {
//        CGFloat r = (CGFloat)(random()%255)/255.;
//        CGFloat g = (CGFloat)(random()%255)/255.;
//        CGFloat b = (CGFloat)(random()%255)/255.;
//        [self.collectionView.cellBackgroundColors addObject:[UIColor colorWithRed:r green:g blue:b alpha:1]];
//    }
    self.collectionView.imageDataArray = self.dataArray;
    [self.collectionView reloadData];
}
- (void)selectIndexWithIndexPath:(NSInteger)indexPath{
    armImageModel *model = self.dataArray[indexPath];
    GDWaterDetailViewController *waterDetailVC = [[GDWaterDetailViewController alloc]init];
    waterDetailVC.model = model;
    [self.navigationController pushViewController:waterDetailVC animated:YES];
}

- (NSMutableArray *)dataArray{
    if(!_dataArray){
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}


@end
